package com.etrans.web.gps.business.report.controller;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.etrans.web.gps.base.controller.BaseController;
import com.etrans.web.gps.business.report.domain.ProcessingStatistics;
import com.etrans.web.gps.business.report.service.GovernmentProcessingStatisticsService;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.grid.FlexiGridUtil;

@Controller
@RequestMapping("/base/report/governmentProcessingStatistics")
public class GovernmentProcessingStatisticsController extends BaseController{
	
	private static final Logger logger = LoggerFactory.getLogger(GovernmentProcessingStatisticsController.class);

	@Autowired
	private GovernmentProcessingStatisticsService governmentProcessingStatisticsService;
	/**
	 * 处理情况统计(政府)
	 * @return
	 */
	@RequestMapping("toGovernmentProcessingStatistics.html")
	public String toGovernmentProcessingStatistics(){
		return "views/business/report/governmentProcessingStatistics";
	}
	
	@RequestMapping(value = "queryProcessingStatistics.html")
	@ResponseBody
	public List<HashMap<String, Object>> queryProcessingStatistics(HttpServletRequest request, HttpServletResponse response, 
			@RequestParam(value="areaIds[]",required=false)Integer[] areaIds, String level, String alarm_type,String startTime, String endTime) {
		List<HashMap<String, Object>> typeList = null;
		try {
			// 参数赋值
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();
			List<Integer> areaList =new ArrayList<>();
			if(areaIds!=null && areaIds.length>0 && !level.equals("1")){
				 areaList = Arrays.asList(areaIds);
				 paramsMap.put("level", level);
			}
			
			paramsMap.put("areaIdList", areaList);
			paramsMap.put("alarm_type", alarm_type);
			paramsMap.put("startTime", startTime);
			paramsMap.put("endTime", endTime);
			typeList = governmentProcessingStatisticsService.queryProcessingTypeStatistics(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return typeList;

	}
	
	@RequestMapping(value = "queryBarListByAreaName.html")
	@ResponseBody
	public List<HashMap<String, Object>> queryBarListByAreaName(String area_name, String alarm_type,
			String startTime, String endTime) {
		List<HashMap<String, Object>> lineList = null;
		try {
			// 参数赋值
			HashMap<String, Object> paramsMap = new HashMap<String, Object>();
			paramsMap.put("area_name", area_name);
			paramsMap.put("alarm_type", alarm_type);
			paramsMap.put("startTime", startTime);
			paramsMap.put("endTime", endTime);
			//通知类型分布（柱状图）
			lineList = governmentProcessingStatisticsService.queryBarListByAreaName(paramsMap);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return lineList;
	}
	
	@RequestMapping(value = "queryTableListByAreaName.html")
	@ResponseBody
	public PageVo queryTableListByAreaName(String paramsGrid) {
		PageVo page = new PageVo();
		try {
			// 参数赋值
			HashMap<String, Object> paramMap = FlexiGridUtil.parseJSONParam(paramsGrid).getWhereParam();
			//列表
			List<ProcessingStatistics> rows = governmentProcessingStatisticsService.queryProcessingStatisticsListByAreaName(paramMap);
			page.setRows(rows);
			page.setPage((int) paramMap.get("page"));
			page.setTotal(governmentProcessingStatisticsService.queryProcessingStatisticsListCount(paramMap));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return page;
	}
	
	@RequestMapping(value = "queryAllAlarmType.html")
	@ResponseBody
	public List<HashMap<String, Object>>  queryAllAlarmType() {
		List<HashMap<String, Object>>  list = new ArrayList<>();
		try {
			//列表
			list = governmentProcessingStatisticsService.queryAllAlarmType();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}
	
	@RequestMapping(value = "exportGovernmentProcessingStatistics.html")
	@ResponseBody
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void exportAreaAnalyzes( HttpServletRequest request, HttpServletResponse response, 
			String area_name, String alarm_type,String title, String StartPage, String EndPage, 
			String PageSize,String colModel, String imgsrc,String startTime, String endTime) throws ParseException {
		//获取参数
		HashMap<String, Object> params = new HashMap<String, Object>();
		List<String> colModelList = JSON.parseObject(colModel, List.class);
		List<String> imgsrcList   = JSON.parseObject(imgsrc, List.class);
		try {
			//分页条件
			int startPage = Integer.parseInt(StartPage);
			int endPage = Integer.parseInt(EndPage);
			int pageSize = Integer.parseInt(PageSize);
			params.put("fromNumber", startPage-1);
			params.put("pageSize", (endPage - startPage+1)*pageSize);	
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取参数失败",e);
		}	
		params.put("area_name", area_name);
		params.put("alarm_type", alarm_type);
		params.put("startTime", startTime);
		params.put("endTime", endTime);
		List<HashMap<String, Object>> rows = new ArrayList<HashMap<String, Object>>();
		List<ProcessingStatistics> list = governmentProcessingStatisticsService.queryProcessingStatisticsListByAreaName(params);
		for (ProcessingStatistics obj : list) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("vehicle_no", obj.getVehicle_no()==null ? "" : obj.getVehicle_no());
			map.put("alarm_name", obj.getAlarm_name()==null ? "" : obj.getAlarm_name());
			map.put("platform_name", obj.getPlatform_name()==null ? "" : obj.getPlatform_name());
			map.put("enterprise_name", obj.getEnterprise_name()==null ? "" : obj.getEnterprise_name());
			map.put("alarm_src", obj.getAlarm_src()==null ? "" : (obj.getAlarm_src()==1?"终端":"平台"));
			map.put("alarm_time", obj.getAlarm_time()==null ? "" : DateUtil.getDateTimeString(obj.getAlarm_time(), null));
			map.put("handle_type", obj.getHandle_type()==null ? "" :(obj.getHandle_type()==1?"系统处理":"人工处理"));
			map.put("handle_name", obj.getHandle_name()==null ? "" : obj.getHandle_name());
			map.put("handle_time", obj.getHandle_time()==null ? "" : DateUtil.getDateTimeString(obj.getHandle_time(), null));
			rows.add(map);
		}
		List rowsList = new ArrayList<>();
		rowsList.add(rows);
		exportExl(request, response,title, removeHeadName(colModelList,-1,-1),rowsList,imgsrcList);
	}
}
